package cn.itrip.common;

import cn.itrip.beans.pojo.ItripUser;
import com.sun.org.apache.xml.internal.security.algorithms.SignatureAlgorithm;
import org.springframework.beans.factory.annotation.Autowired;

import javax.crypto.spec.SecretKeySpec;
import javax.xml.bind.DatatypeConverter;
import java.security.Key;
import java.util.Date;

public class TokenUtil {
    @Autowired
    RedisUtil redisUtil;

    //创建token
    //规定命名：token+userid+usercode+创建时间（唯一）
    public String createToken(ItripUser user) {
        String token = "token:" + user.getId() + "-" + user.getUserCode() + "-" + new Date();
        return token;
    }


    //清除token
    public boolean clean(String token) {
        if (isExist(token)) {
            redisUtil.del(new String[]{token});
            return true;
        }
        return false;
    }


    //验证token
    public boolean isExist(String token) {
        if (redisUtil.hasKey(token)) {
            return true;
        }
        return false;
    }


}
